<?php

abstract class Base_Model
{
    protected $_dbh;
    protected $_totalPages;
	
	/**
	 * @author kiote
	 */
	protected static $_totalRows;
    
    public function __construct()
    {
        //TODO: а если это не объект DbSimple
        $this->_dbh = Framework_Registry::get('dbh');
    }
	
	/**
	 * @author kiote
	 */
	public static function getTotalRows()
	{
		$_config = Framework_Registry::get('config');
		$onpage = $_config->admin->on_page;
		$pages = ceil (self::$_totalRows / $onpage);
		return $pages;
	}
    
    public function getTotalPages()
    {
        return $this->_totalPages;
    }
    
    protected function _setTotalPages($page = 0, $sum = 10)
    {
        //Сколько всего записей
        $sql = "SELECT FOUND_ROWS()";
        $count_rows = $this->_dbh->selectCell($sql);
        $count_pages = ceil($count_rows/$sum);

        if ($count_rows <= $sum) return false;
        
        // > пейджинг
        $start_page = $page - 10;
        if ($start_page < 0) {
            $start_page = 0;
        }
        $end_page = $page + 10;
        if ($end_page > $count_pages) {
            $end_page = $count_pages;
        }
        for ($i=$start_page; $i<$end_page; $i++) {
            $pages[$i] = $i+1;
        }
        
        $this->_totalPages = $pages;
    }
}
    